<template>
  <div class="app-container-sm">
      <el-card class="card-margin-bottom">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px" class="form-minus-bottom">
      <base-select v-model="queryParams.baseId"></base-select>
      <el-form-item label="店铺名称" prop="shopName">
        <el-input
          v-model="queryParams.shopName"
          placeholder="请输入店铺名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
        <el-form-item class="fr">
            <el-button
            v-if="!isAdmin"
                    type="primary"
                    plain
                    icon="el-icon-plus"
                    size="mini"
                    @click="handleAdd"
                    v-hasPermi="['trace:shop:add']"
            >新增</el-button>
            <el-button
                    type="warning"
                    plain
                    icon="el-icon-download"
                    size="mini"
                    @click="handleExport"
                    v-hasPermi="['trace:shop:export']"
            >导出</el-button>
        </el-form-item>
    </el-form>
      </el-card>
<el-card class="card-padding-bottom">
    <el-table v-loading="loading" :data="shopList" >
      <el-table-column label="店铺名称" align="center" prop="shopName" />
      <el-table-column label="店铺类型" align="center" prop="shopType">
          <template #default="scope">
            <dict-tag :options="dict.type.trace_shop_type" :value="scope.row.shopType"/>
        </template>
      </el-table-column>
      <el-table-column label="店铺图片" align="center" prop="shopImg" width="180">
          <template v-slot:default="{ row }">
              <div class="image" @click="previewImage(`${ image.baseUrl  + row.shopImg }`,row)">
                  <img style="width:50px;height:50px;" :src="`${ image.baseUrl  + row.shopImg }`" />
              </div>
          </template>
      </el-table-column>
      <el-table-column label="店铺地址" align="center" prop="shopAddress" />
      <!-- <el-table-column label="店铺坐标" align="center" prop="shopCoordinate" /> -->
      <el-table-column label="店铺联系人" align="center" prop="shopContacts" />
      <el-table-column label="店铺联系方式" align="center" prop="shopTel" />
      <el-table-column label="店铺链接" align="center" prop="shopUrl" >
          <template #default="{row}">
              <el-link :href="row.shopUrl" type="primary" target="_blank">店铺链接</el-link>
          </template>
      </el-table-column>
      <el-table-column label="是否可用" align="center" prop="status">
        <template slot-scope="scope">
          <!-- <dict-tag :options="dict.type.trace_status" :value="scope.row.status"/> -->
          <el-switch
                v-model="scope.row.status"
                @change="handleStatus(scope.row)"
                active-value="0"
                inactive-value="2">

            </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="small"
            type="warning"
            class="padding-5"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['trace:shop:edit']"
          >修改</el-button>
          <el-button
            size="small"
            type="danger"
            class="padding-5"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['trace:shop:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
</el-card>
    <!-- 添加或修改店铺对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="店铺名称" prop="shopName">
          <el-input v-model="form.shopName" placeholder="请输入店铺名称" />
        </el-form-item>
        <el-form-item label="店铺类型" prop="shopType" >
          <el-select v-model="form.shopType" placeholder="请选择店铺类型" class="display-block">
            <el-option
              v-for="dict in dict.type.trace_shop_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="店铺图片" prop="shopImg">
          <imageUpload v-model="form.shopImg"/>
        </el-form-item>
        <el-form-item label="店铺地址" prop="shopAddress">
          <el-input v-model="form.shopAddress" placeholder="请输入店铺地址" />
        </el-form-item>
        <el-form-item label="店铺坐标" prop="shopCoordinate">
          <point-select v-model="form.shopCoordinate" :isDrawLand="false"  ></point-select>
        </el-form-item>
        <el-form-item label="店铺联系人" prop="shopContacts">
          <el-input v-model="form.shopContacts" placeholder="请输入店铺联系人" />
        </el-form-item>
        <el-form-item label="店铺联系方式" prop="shopTel">
          <el-input v-model="form.shopTel" placeholder="请输入店铺联系方式" />
        </el-form-item>
        <el-form-item label="店铺链接" prop="shopUrl">
          <el-input v-model="form.shopUrl" placeholder="请输入店铺链接" />
        </el-form-item>
        <el-form-item label="店铺描述" prop="shopDes">
          <el-input v-model="form.shopDes" type="textarea" placeholder="请输入店铺描述" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
        <!-- 图片预览对话框 -->
    <el-dialog :title="image.title" :visible.sync="image.open" width="240px">
        <img style="width:200px;height:200px;" :src="image.imgUrl" />
    </el-dialog>
  </div>
</template>

<script>
import { listShop, getShop, delShop, addShop, updateShop } from "@/api/trace/shop";
import PointSelect from '@/views/iot/device/components/PointSelect';
import { mapGetters } from 'vuex';

export default {
  name: "Shop",
  dicts: ['trace_status','trace_shop_type'],
  components:{PointSelect},
  data() {
    return {
      //图片预览
      image:{
          baseUrl:window.location.origin+process.env.VUE_APP_BASE_API,
          open:false,
          imgUrl:'',
          title:''
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 店铺表格数据
      shopList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        shopName: null,
        baseId:null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        shopName: [
          { required: true, message: "店铺名称不能为空", trigger: "blur" }
        ],
        shopType: [
          { required: true, message: "店铺类型不能为空", trigger: "change" }
        ],
        shopImg: [
          { required: true, message: "店铺图片不能为空", trigger: "blur" }
        ],
        shopAddress: [
          { required: true, message: "店铺地址不能为空", trigger: "blur" }
        ],
        shopCoordinate: [
          { required: true, message: "店铺坐标不能为空", trigger: "blur" }
        ],
        shopContacts: [
          { required: true, message: "店铺联系人不能为空", trigger: "blur" }
        ],
        shopTel: [
          { required: true, message: "店铺联系方式不能为空", trigger: "blur" }
        ],
        shopUrl: [
          { required: true, message: "店铺链接不能为空", trigger: "blur" }
        ],
        shopDes: [
          { required: true, message: "店铺描述不能为空", trigger: "blur" }
        ],
        remark: [
          { required: true, message: "备注不能为空", trigger: "blur" }
        ],
      }
    };
  },
  computed:{
        ...mapGetters(['isAdmin'])
   },
  created() {
    this.getList();
  },
  methods: {
    /** 查询店铺列表 */
    getList() {
      this.loading = true;
      listShop(this.queryParams).then(response => {
        this.shopList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        shopId: null,
        shopName: null,
        shopType: null,
        shopImg: null,
        shopAddress: null,
        shopCoordinate: null,
        shopContacts: null,
        shopTel: null,
        shopUrl: null,
        shopDes: null,
        remark: null,
        status: "0",
        orderNum: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加店铺";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const shopId = row.shopId || this.ids
      getShop(shopId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改店铺";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.shopId != null) {
            updateShop(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addShop(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const shopIds = row.shopId || this.ids;
      this.$modal.confirm('是否确认删除店铺编号为"' + shopIds + '"的数据项？').then(function() {
        return delShop(shopIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 图片预览按钮操作 */
    previewImage(imgUrl,row){
        this.image.title = row.version
        this.image.open = true;
        this.image.imgUrl = imgUrl;
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('agriculture/shop/export', {
        ...this.queryParams
      }, `shop_${new Date().getTime()}.xlsx`)
    },
    /* 店铺状态修改 */
    async handleStatus(row) {
      await updateShop({shopId:row.shopId,status:row.status});
      await this.$modal.msgSuccess('设置成功');
      this.getList();
    },
  }
};
</script>
